package com.express.mvc;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: laixiangdong
 * @Date 2020/12/02
 */
public class HandlerMapping {
    private static Map<String, MVCMapping> data = new HashMap<>();

    public static void load(InputStream is) {
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collection<Object> values = properties.values();
        // 取出配置文件中的类名
        for (Object o : values) {
            String className = (String) o;
            try {
                Class c = Class.forName(className);
                // 创建每个类的对象
                Object obj = c.getConstructor().newInstance();
                Method[] methods = c.getMethods();
                for (Method m : methods) {
                    Annotation[] annotations = m.getAnnotations();
                    if (annotations != null) {
                        for (Annotation annotation : annotations) {
                            if (annotation instanceof ResponseBody) {
                                MVCMapping mvcMapping = new MVCMapping(obj, m, ResponseType.TEXT);
                                MVCMapping put = data.put(((ResponseBody) annotation).value(), mvcMapping);
                                if (put != null) {
                                    // 存在重复的地址
                                    throw new RuntimeException("请求地址重复" + ((ResponseBody) annotation).value());
                                }
                            } else if (annotation instanceof ResponseView) {
                                MVCMapping mvcMapping = new MVCMapping(obj, m, ResponseType.VIEW);
                                MVCMapping put = data.put(((ResponseView) annotation).value(), mvcMapping);
                                if (put != null) {
                                    // 存在重复的地址
                                    throw new RuntimeException("请求地址重复" + ((ResponseBody) annotation).value());
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static MVCMapping get(String uri) {
        return data.get(uri);
    }

    public static class MVCMapping {
        private Object obj;
        private Method method;
        private ResponseType type;

        public MVCMapping(Object obj, Method method, ResponseType type) {
            this.obj = obj;
            this.method = method;
            this.type = type;
        }

        public MVCMapping() {
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public Method getMethod() {
            return method;
        }

        public void setMethod(Method method) {
            this.method = method;
        }

        public ResponseType getType() {
            return type;
        }

        public void setType(ResponseType type) {
            this.type = type;
        }
    }
}
